package com.fiona.core.remoting.model;

import java.net.InetSocketAddress;
import java.util.Objects;

import com.fiona.common.constant.FionaConstant;

import cn.hutool.core.net.NetUtil;

/**
 * @author huacongnan
 * @date 2021/3/12
 */
public class Service {
	/**
	 * 服务名称
	 */
	private String name;
	/**
	 *
	 * 服务接口
	 */
	private String interfaceName;
	/**
	 *
	 * 服务版本号
	 */
	private String version;
	/**
	 * 服务地址，格式：ip:port
	 */
	private String address;
	/**
	 * 权重，越大优先级越高
	 */
	private Integer weight;
	/**
	 * 服务Class
	 */
	private Class<?> clazz;
	/**
	 * 具体服务
	 */
	private Object obj;

	public Integer getWeight() {
		return weight;
	}

	public void setWeight(Integer weight) {
		this.weight = weight;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	@Override
	public String toString() {
		return "Service{" + "name='" + name + '\'' + ", interfaceName='" + interfaceName + '\'' + ", version='"
				+ version + '\'' + ", address='" + address + '\'' + ", weight=" + weight + '}';
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		Service service = (Service) o;
		return Objects.equals(name, service.name) && Objects.equals(interfaceName, service.interfaceName)
				&& Objects.equals(version, service.version) && Objects.equals(address, service.address)
				&& Objects.equals(weight, service.weight);
	}

	@Override
	public int hashCode() {
		return Objects.hash(name, interfaceName, version, address, weight);
	}

	/**
	 * 返回如:com.fiona.example.service.HelloService/serviceName1/1.0.0/ip:port
	 * 
	 * @return
	 */
	public String getUri() {
		return interfaceName.concat(FionaConstant.BACKSLASH).concat(name).concat(FionaConstant.BACKSLASH)
				.concat(version).concat(FionaConstant.BACKSLASH).concat(address);
	}

	/**
	 * 获取服务ID：接口名-服务名-版本号
	 * 
	 * @return
	 */
	public String getServiceId() {
		return interfaceName + FionaConstant.STRIKETHROUGH + name + FionaConstant.STRIKETHROUGH + version;
	}
}
